﻿package net.jmp0.toast.levels 
{
	
	import flash.events.Event;
	import flash.net.URLLoader;
	import flash.net.URLLoaderDataFormat;
	import flash.net.URLRequest;
	import flash.utils.ByteArray;
	import punk.Actor;
	
	/**
	 * ...
	 * @author Thodd
	 */
	public class LevelLoader extends Actor
	{
		private static var allLevels:Array = new Array();
		
		private static var levelList:XML;
		private static var levelUrls:Array = new Array();
		
		private static var instance:LevelLoader;
		
		private static var loadingStarted:Boolean = false;
		
		public function LevelLoader() {
			FP.world.add(this);
		}
		
		
		public static function getInstance():LevelLoader {
			if (instance == null) {
				instance = new LevelLoader();
			}
			return instance;
		}
		
		
		/**
		 * Loads the levellist, after the list is loaded all levels will be loaded
		 * And after that the first level in the list is started
		 */
		public function loadLevelList():void {
			loadXMLFile("levellist.xml", levelListComplete);
		}
		
		
		/**
		 * Loads a XML file from an URL
		 * @param	url the url to load the xml file from
		 * @param	callback a function that is called after the loading is finished
		 */
		public function loadXMLFile(url:String, callback:Function):void{
			var urlRequest:URLRequest = new URLRequest(url);
			var urlLoader:URLLoader = new URLLoader();
			urlLoader.dataFormat = URLLoaderDataFormat.TEXT;
			urlLoader.addEventListener(Event.COMPLETE, callback);
			urlLoader.load(urlRequest);
		}
		
	
		/**
		 * A callback function for loadXMLFile
		 * @param	evt The Event that called this function
		 */
		private function levelListComplete(evt:Event):void {
			levelList = new XML(evt.target.data.toString());
			trace(evt.target.data);
			
			//when we got the levellist we're going to load each level in the list
			var e:XML;
			for each (e in levelList.level) {
				levelUrls.push(e.@file);
				trace(e.@file);
			}
			
			//Starting to load the real Levelfiles
			loadingStarted = true;
			loadLevels();

		}
		
		/**
		 * Iterating all URLs and retrieving the levels
		 */
		private function loadLevels():void {
			
			var url:String
			for each(url in levelUrls) {
				loadXMLFile("levels/"+url, levelLoadingComplete);
			}
			
			//this function will be called when a level was completly loaded
			function levelLoadingComplete(evt:Event):void {
				allLevels.push(new XML(evt.target.data.toString())); //retrieving the level xml data
				trace("level loaded:\n" + evt.target.data);
			}
			
		}
		
		
		override public function update():void {

			//If the loading was started we need to check for the moment that all levels have been loaded
			if (loadingStarted) {
				//this is done by comparing the currently loaded levels and the total number of URLs to load levels from
				if (allLevels.length == levelUrls.length) {
					trace("done loading " + allLevels.length + " level(s)");
					levelUrls = null;
					loadingStarted = false;
					
					var lt:LevelTracker = LevelTracker.getInstance();
					lt.setLevelList(allLevels);
					lt.switchToLevel(lt.next());
				}
			}
			
		}

	}

}